﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.CMS.Process;
using TjZheng.Controls;
using TjZheng.Framework;
using TjZheng.Logger.Business;
using TjZheng.Logger.DataEntity;

namespace TjZheng.CMS.Controllers
{
    public class LogInfoController : CMSController
    {
        BizLogInfo objBizLogInfo { get; set; }
        public LogInfoController()
        {
            objBizLogInfo = new BizLogInfo();
        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult LogInfoIndex()
        {
            ViewBag.Title = TransferTwoID;
            return View();
        }
        [QueryMsSqlFilter]
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ListJson(int page, int rows)
        {
            string lcIds = GetString("LcIds");
            if (lcIds.IsNoNull())
            {
                QueryResult.Condition = QueryResult.Condition.AndCondition(lcIds.FormatSelf("LcId in({0})"));
            }
            return Json(objBizLogInfo.GetPageResult(page - 1, rows, QueryResult.Condition, QueryResult.Parameters, "LogDate desc"));
        }
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult LogModuleList()
        {
            BizLogModule objBizLogModule = new BizLogModule();
            var objList = objBizLogModule.GetList("");
            InvokeResult objInvokeResult = new InvokeResult();
            if (objList.IsNull())
            {
                objInvokeResult.ResultCode = "DataEmpty";
                return Json(objInvokeResult);
            }
            objInvokeResult.Data = objList.Select(c => new SelectListItem { Text = c.ModuleCode, Value = c.ModuleCode });
            return Json(objInvokeResult, "text/plain");
        }
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult DetailPage(long id)
        {
            LogInfo objLogInfo = objBizLogInfo.GetRecord(id) ?? new LogInfo();
            return View(objLogInfo);
        }
        public ActionResult Delete(string id)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (id.IsNull())
            {
                objInvokeResult.ResultCode = "IDNull";
                objInvokeResult.ResultMessage = "请先选择一条数据";
                return Json(objInvokeResult, "text/plain");
            }
            objBizLogInfo.DeleteIDString(id);
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "删除成功";
            return Json(objInvokeResult, "text/plain");
        }
        public ActionResult ClearLog()
        {
            return View();
        }
        public ActionResult SaveClear(DateTime clearTime, string logModule)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            BizLogInfo objBizLogInfo = new BizLogInfo();
            if (logModule.IsNull())
            {
                objBizLogInfo.Delete(string.Format("LogDate<='{0}' AND LcId in({1})", clearTime.ToString("yyyy-MM-dd HH:mm:ss"), TransferID));
            }
            else
            {
                objBizLogInfo.Delete(string.Format("LogDate<='{0}' AND LcId in({1}) AND ModuleType=@ModuleType", clearTime.ToString("yyyy-MM-dd HH:mm:ss"), TransferID), "ModuleType".CreateSqlParameters(logModule));
            }
            objInvokeResult.ResultMessage = "删除成功";
            return Json(objInvokeResult, "text/plain");
        }
    }
}
